package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsUserMapper;
import com.qf.pojo.DtsUser;
import com.qf.service.DtsUserService;
import com.qf.vo.DayStatis;
import com.qf.vo.StatVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * @author 邢式瑞
 * @version 1.0
 * @project E-commercebackGroundManagementSystem
 * @date 2022/9/21 10:00
 */
@Service
public class DtsUserServiceImpl implements DtsUserService {
@Autowired
     private DtsUserMapper dtsUserMapper;

    @Override
    public long selectUserCount() {
        return dtsUserMapper.selectCount(new QueryWrapper<DtsUser>().eq("deleted", false));
    }

    @Override
    public List<DayStatis> selectOrderCnt(int statisDayRange) {
        return dtsUserMapper.selectOrder(statisDayRange);
    }

    @Override
    public Page<DtsUser> findByPage(String username,String phone,int pageNum, int pageSize, String sort, String order) {
        Page<DtsUser> dtsUserPage = new Page<>(pageNum, pageSize);
        QueryWrapper<DtsUser> dtsUserQueryWrapper = new QueryWrapper<>();
        dtsUserQueryWrapper.orderBy(true,order.equals("asc"),sort).like(!StringUtils.isEmpty(username),"username",username).like(!StringUtils.isEmpty(phone),"mobile",phone);
        return dtsUserMapper.selectPage(dtsUserPage, dtsUserQueryWrapper);

    }

    @Override
    public DtsUser selectUserById(Integer id) {
        return dtsUserMapper.selectById(id);
    }

    @Override
    public StatVo selectDayAndUserCount() {
        List<DayStatis> dayStatisList = dtsUserMapper.selectDayDanfCount();
        StatVo statVo = new StatVo();

        String[] strings = {"day", "users"};

        List list = new ArrayList();
        statVo.setColumns(strings);
        for (DayStatis dayStatis : dayStatisList) {
            Map<String, Object> map = new HashMap<>();
                map.put(strings[0],dayStatis.getDayStr());
                map.put(strings[1],dayStatis.getCnts());
                list.add(map);

        }
     statVo.setRows(list);

          return statVo;
    }
}
